package handler;

import beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import service.LoginService;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller// 使此类成为一个SpringMvc Controller对象，分发处理器会扫描使用该注解的类的方法
@RequestMapping("/Login")//映射请求
public class Login {
    @Autowired// 属性自动注入
    private LoginService loginService;
    @RequestMapping("/loginUser")
    public String loginUser(User user1 , Model model, HttpServletRequest request){
        HttpSession session=request.getSession();
        User user=loginService.login(user1);
        if (user==null){
            model.addAttribute("cc_error", "用户名或密码或职位错误");
            return "index";
        }
        // 登录成功后把用户信息放在请求域中，防止拦截器拦截请求；
        request.setAttribute("user", user);
        session.setAttribute("user",user);
        model.addAttribute("user",user);
        return user.getFlag()+"/"+user.getFlag()+"_login";
    }
}
